package com.hotelsystem.util;

import com.hotelsystem.dao.LoggingMapper;
import com.hotelsystem.pojo.Employee;
import com.hotelsystem.pojo.Logging;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 日志工具类
 */
@Component
public class LoggingUtils {

    @Resource(name = "loggingMapper")
    private LoggingMapper loggingMapper;

    /**
     * 添加日志
     * @param session 会话
     * @param message 日志信息
     * @return
     */
    public Logging addLogging(HttpSession session, String message){
        Logging logging = null;
        Employee emp = (Employee) session.getAttribute(ConstantValue.LOGIN_USER);
        try {
            logging = new Logging(null, message, new Date(), emp.getUsername());
            loggingMapper.insert(logging);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return logging;
    }
}
